<?php
namespace app\controllers;

use app\models\dao\RoleNode;
use app\models\RoleModel;
use Yii;
use app\models\dao\Role;
use app\common\widgets\Tools;
use app\models\User;
use app\models\dao\RoleAccess;
use yii\data\Pagination;

/**
 * Site controller
 */
class RoleController extends BaseController {
	public $role;

	public function init() {
		parent::init();
		$this->role = new RoleModel();
	}

	public function actionIndex() {
		return $this->redirect(\Yii::$app->urlManager->createUrl(['role/lists']));
	}

	public function actionLists() {
		$ary_page_data = [];
		$filter = [];
		$page = \Yii::$app->request->get('page',1);
		$keyword = \Yii::$app->request->post('keyword');
		$page_params = [
			'pageno'=> $page,
			'pagesize'=> $this->_limit,
		];
		$query = [];
		$filter['page'] = $page;
		$filter['keyword'] = '';
		if(!empty($keyword)) {
			$query = [
				'where'=>['name'=>$keyword],
			];
			$filter['keyword'] = $keyword;
		}
		$ary_data = $this->role->getAll($query, $page_params);

		$pagination = new Pagination([
				'defaultPageSize' => $this->_limit,
				'totalCount' => $ary_data['total_results'],
		]);

		$ary_page_data['data'] = $ary_data['datalist'];
		$ary_page_data['total'] = $ary_data['total_results'];
		$ary_page_data['limit'] = $this->_limit;
		$ary_page_data['filter'] = $filter;
		$ary_page_data['pages'] = $pagination;
//		echo "<pre>";print_r($pagination);exit;
		return $this->render('lists', $ary_page_data);
	}

	public function actionAdd() {
		$ary_page_data = [];
		$ary_data = $this->role->getMenus();
		$ary_page_data['data'] = $ary_data;
//		echo "<pre>";print_r($ary_data);exit;
		return $this->render('permission', $ary_page_data);
	}

	public function actionEdit() {
		$ary_page_data = [];
		$selectNav = [];
		$selectNode = [];
		$int_id = (int)\Yii::$app->request->get('id', 0);
		$arr_role_data = $this->role->getOne($int_id);
		if(empty($arr_role_data)) {
			Tools::sendJson(40000301, "数据错误，请重试");
		}
		$ary_data = $this->role->getMenus($arr_role_data['type']);

		//获取已选择权限
		$accessNode = RoleAccess::find()->where(['role_id' => $int_id])->asArray()->all();
		if(!empty($accessNode)) {
			foreach($accessNode as $nodeList) {
				$nodeArr = RoleNode::find()->where(['id' => $nodeList['node_id']])->asArray()->one();
				if(!in_array($nodeArr['id'], $selectNode)) {
					$selectNode[] = $nodeArr['id'];
				}
				if(!in_array($nodeArr['parent_id'], $selectNode)) {
					$selectNode[] = $nodeArr['parent_id'];
				}
				if(!in_array($nodeArr['nav_id'], $selectNav)) {
					$selectNav[] = $nodeArr['nav_id'];
				}
			}
		}
//		echo "<pre>";print_r($ary_data);exit;
		$ary_page_data['selectNav'] = $selectNav;
		$ary_page_data['selectNode'] = $selectNode;
		$ary_page_data['data'] = $ary_data;
		$ary_page_data['info'] = $arr_role_data;
		return $this->render('edit', $ary_page_data);
	}

	public function actionSave() {
		$roleName = \Yii::$app->request->post('role_name');
		$description = \Yii::$app->request->post('description');
		$status = (int)\Yii::$app->request->post('status', 1);
		$access_node = \Yii::$app->request->post('access_node');
		if(empty($roleName)) {
			Tools::sendJson("40000201", "请输入角色名称");exit;
		}
		if(empty($access_node)) {
			Tools::sendJson("40000202", "请选择对应角色权限!");exit;
		}
		$datetime = date("Y-m-d H:i:s");
		$data = array();
		$data['name'] = $roleName;
		$data['description'] = $description;
		$data['status'] = $status;
		$data['create_time'] = $datetime;
		$result = $this->role->saved($data, $access_node);
		if(true === $result['status']) {
			Tools::sendJson(0, "保存成功", \Yii::$app->urlManager->createUrl(['role/lists']));
		} else {
			Tools::sendJson(40000205, "保存失败");
		}
	}

	public function actionUpdate() {
		$int_role_id = (int)\Yii::$app->request->post('role_id');
		$roleName = \Yii::$app->request->post('role_name');
		$description = \Yii::$app->request->post('description');
		$status = (int)\Yii::$app->request->post('status', 1);
		$access_node = \Yii::$app->request->post('access_node');
		$ary_data = $this->role->getOne($int_role_id);
		if(empty($roleName)) {
			Tools::sendJson("40000201", "请输入角色名称");exit;
		}
		if(empty($ary_data)) {
			Tools::sendJson(40000401, "数据错误，请重试");
		}

		$data = array();
		$data['role_id'] = $int_role_id;
		$data['name'] = $roleName;
		$data['description'] = $description;
		$data['status'] = $status;
		$result = $this->role->updated($data, $access_node);
		if(true === $result['status']) {
			Tools::sendJson(0, "更新成功", \Yii::$app->urlManager->createUrl(['role/lists']));
		} else {
			Tools::sendJson(40000205, "更新失败");
		}
	}

	public function actionDodel(){
		$int_id = (int)\Yii::$app->request->get('id');
		$where = array(
			'id'   => $int_id,
			'flag'	=> 1
		);
		$ary_role = Role::find()->where($where)->one();
		if(empty($ary_role)) {
			Tools::sendJson("40000101", "该角色不存在或已删除");
		}
		$int_role_id = intval($ary_role['id']);
		if(!empty($int_role_id) && $int_role_id >= 0){
			$ary_user = User::find()->where(['role_id' => $int_role_id, 'flag' => 1])->asArray()->all();
			if(!empty($ary_user)) {
				Tools::sendJson("40000109", "该角色已被使用,无法删除!");
			}
			$data = array(
				'id'    => $int_role_id,
				'flag'  => 0,
			);
			$ary_result = $this->role->modify($data);
			if(FALSE === $ary_result){
				Tools::sendJson("40000110", "删除失败");
			}  else {
				$del_condition = "role_id =:role_id";
				$del_params = array(
					':role_id' => $int_role_id,
				);
				$del_result = RoleAccess::deleteAll($del_condition,$del_params);
				if (false === $del_result) {
					Tools::sendJson("40000111", "删除角色信息失败，请重试");
				}
				Tools::sendJson("0", "删除成功");
			}
		}else{
			Tools::sendJson("40000108", "该角色不存在");
		}
	}
}